Ext.ns('MyExt.formUtil');

// ========== 初始化字段信息 开始==============
MyExt.formUtil.getForm = function(structure){
	var len = structure.length
	var oField = [];
	
	for (var i = 0; i < len; i++) {
		var c = structure[i];
		var type;
		var ds;
		c.type = c.type || 'string'; // 默认类型为string
		switch (c.type) {
		   case 'string' :
				oField[oField.length] = {
						xtype : 'textfield',
						//id : tablename + '_' + c.name,
						name: c.name,
						fieldLabel : c.header,
						addIsShow:c.addIsShow,
						editIsShow:c.editIsShow,
						anchor : '90%',
					//	regex: /^[A-Za-z]+[0-9]+[A-Za-z0-9]*|[0-9]+[A-Za-z]+[A-Za-z0-9]*$/,
					//	regexText: '密码必须由8位字母数字组合而成',
						vtype : c.vtype,
					//	vtypeText : c.vtypeText||"",
						allowBlank : c.required ? false : true
				}; break;
		 case 'number' :
				oField[oField.length] = {
						xtype : 'num',
						//id : tablename + '_' + c.name,
						name: c.name,
						fieldLabel : c.header,
						anchor : '90%',
						vtype : c.vtype,
						editIsShow:c.editIsShow,
						addIsShow:c.addIsShow,
						allowBlank : c.required ? false : true
				};
				break;
		case 'date' :
			oField[oField.length] = {
				xtype : 'datefield',
				//id : tablename + '_' + c.name,
				name: c.name,
				fieldLabel : c.header,
				format : 'Y-m-d',
				anchor : '90%',
				editIsShow:c.editIsShow,
				addIsShow:c.addIsShow,
				allowBlank : c.required ? false : true
			};break;
		case 'combo' :
			// 初始化下拉列表数据
			c.value = c.value || 'value';
			c.text = c.text || 'text';
			ds = MyExt.formUtil.getComboStore(c);
	
			oField[oField.length] = {
				xtype : 'combo',
				//hiddenName : tablename + '_' + c.name,
				hiddenName: c.name,
				fieldLabel : c.header,
				anchor : '90%',
				store : ds,
				displayField : c.text,
				valueField : c.value,
				typeAhead : true,
				triggerAction : 'all',
				selectOnFocus : true,
				//hideTrigger:c.editIsReadOnly,
				mode : 'local',
				editIsShow:c.editIsShow,
				editIsReadOnly:c.editIsReadOnly,
				addIsShow:c.addIsShow,
				allowBlank : c.required ? false : true,
				listeners : {
					'blur' : function() {
						this.setValue(this.getValue())
					}
					,"expand":function(t){
						if(t.el.dom.readOnly){
							t.collapse();
						}
					}
				}
		};break;
		case 'label' :
			oField[oField.length] = {
					xtype : 'textfield',
					//disabled : true,
					//readOnly :true,
					name: c.name,
					//id : tablename + '_' + c.name,
					fieldLabel : c.header,
					anchor : '90%',
					vtype : c.vtype,
					editIsShow:c.editIsShow,
					addIsShow:c.addIsShow
					//,value : '系统将自动生成'
		};break;
	    case 'textarea' :
			oField[oField.length] = {
					xtype : 'textarea',
					height:70,
					name: c.name,
					//disabled : true,
					//autoScroll :true,
					vtype : c.vtype,
					addIsShow:c.addIsShow,
					//id : tablename + '_' + c.name,
					fieldLabel : c.header,
					anchor : '90%'
		};break;
		case 'twoColumn' :
			oField[oField.length] = {
		        			xtype:'radiogroup',
		        			//title:'其他信息',
		        			//anchor:"96%",
		        			//autoHeight:true,
		        			layout:"column",
		        			columns: 2,
		        			addIsShow:c.addIsShow,
		        			name:c.name,
		        			fieldLabel : c.header,//columnWidth: .45,xtype:'radio',
		        			items:[
		        					{boxLabel: c.fobj[0].boxLabel, name:c.name,inputValue: c.fobj[0].value},
		                    		{boxLabel: c.fobj[1].boxLabel, name:c.name,inputValue: c.fobj[1].value}
		        			]
		   }; break;
		}//endof switch
	}		
	return oField;
	
}

MyExt.formUtil.getComboStore= function(o){
				var url = o.url;
				var fields = o.value + ',' + o.text;
				o.value = o.value || 'value';
				o.text = o.text || 'text';
				var ds = null;
				if (typeof o.fobj != 'object') {
						var reader = new Ext.data.JsonReader({
									totalProperty : 'Count',
									root:'List',
									fields:[ 
											{name : o.value}, 
											{name : o.text}
									]
						 });
						ds = new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
										url : url
							}),
							reader : reader
						});
			
			
						ds.on('beforeload', function() {
						// Ext.MessageBox.wait('正在加载数据请稍后。。。。。。','请稍后')
								var para = {
									action : 'show',
									pagesize : '999',
									name : o.fobj,
									fields : fields
								};
								Ext.apply(ds.baseParams, para);
						});
				} else {
					//	var ofobj = [["ddd","显示值"]];
						//alert(o.fobj);
						//o.fobj = [{value:"0",text:"挂失"},{value:"1",text:"正常"}];
						ds = new Ext.data.JsonStore({
						    //url: 'get-images.php',
						    //root: 'images',
						    fields: [ {name:o.value, mapping:o.value},  {name:o.text, mapping:o.text}],
						    data:o.fobj
						});
					
						/*ds = new Ext.data.SimpleStore({
									fields:[o.value,o.text],
									data:o.fobj
						});*/
				}
				/*ds.load({
							params : {
								start : 0,
								limit : 10
							}
				});*/
				return ds;
}

MyExt.formUtil.getCombo = function(c){
		var ds = MyExt.formUtil.getComboStore(c);
		c.value = c.value || 'value';
		c.text = c.text || 'text';
		//alert(ds.getCount());
		return new Ext.form.ComboBox({
					xtype : 'combo',
					//hiddenName : tablename + '_' + c.name,
					hiddenName: c.name,
					fieldLabel : c.header,
					anchor : '90%',
					store : ds,
					displayField : c.text,
					valueField : c.value,
					typeAhead : true,
					triggerAction : 'all',
					selectOnFocus : true,
					//hideTrigger:c.editIsReadOnly,
					mode : 'local',
					readOnly:true,
					//editIsShow:c.editIsShow,
					//editIsReadOnly:c.editIsReadOnly,
					//addIsShow:c.addIsShow,
					allowBlank : c.required ? false : true,
					listeners : {
						'blur' : function() {
							this.setValue(this.getValue())
						}
						/*,"expand":function(t){
							if(t.el.dom.readOnly){
								t.collapse();
							}
						}*/
					}
			})

};
